﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessObjects;
using DataObjects.EntityFramework.ModelMapper;
using System.Linq.Dynamic;

namespace DataObjects.EntityFramework.Implementation
{
    public class EntityTrafficDao : ITrafficDao
    {
        public Traffic GetTraffic(int TrafficId, int SupplierID)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return Mapper.Map(context.TrafficEntities.FirstOrDefault(t => ((t.TrafficId == TrafficId) && (t.SupplierId == SupplierID))));
            }
        }
        public List<Traffic> GetTraffic(string sortExpression = "TrafficId ASC")
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var list = new List<Traffic>();

                var traffics = context.TrafficEntities.AsQueryable().OrderBy(sortExpression).ToList();
                foreach (var traffic in traffics)
                    list.Add(Mapper.Map(traffic));

                return list;
            }
        }
        public void InsertTraffic(Traffic traffic)
        {
            var entity = Mapper.Map(traffic);

            using (var context = DataObjectFactory.CreateContext())
            {
                try 
                {
                    context.TrafficEntities.AddObject(entity);
                    context.SaveChanges();

                    // update business object with new version and id
                    traffic.TrafficId = entity.TrafficId;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                
                //country.Version = Convert.ToBase64String(entity.Version);
            }
        }
        public void UpdateTraffic(Traffic traffic)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.TrafficEntities.Where(p => p.TrafficId == traffic.TrafficId).SingleOrDefault();

                entity.TrafficId = traffic.TrafficId;
                entity.SupplierId = traffic.SupplierId;
                entity.ServiceId = traffic.ServiceId;
                entity.TrafficTypeId = traffic.TrafficTypeId;
                entity.DriverId = traffic.DriverId;
                entity.SubDriverId_1 = traffic.SubDriverId1;
                entity.SubDriverId_2 = traffic.SubDriverId2;
                entity.Layer = traffic.Layer;
                entity.L1RowNumber = traffic.L1RowNumber;
                entity.L1NCPR = traffic.L1NCPR;
                entity.L2RowNumber = traffic.L2RowNumber;
                entity.L2NCPR = traffic.L2NCPR;
                entity.Description = traffic.Description;
                entity.Picture = traffic.Picture;
                //entity.Version = traffic.Version;
                traffic.Version = Convert.ToBase64String(entity.Version);
                entity.Status = traffic.Status;

                context.TrafficEntities.ApplyCurrentValues(entity);
                context.SaveChanges();

                // Update business object with new version
                
            }
        }
        public void DeleteTraffic(Traffic traffic)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.TrafficEntities.Where(t => t.TrafficId == traffic.TrafficId).SingleOrDefault();
                context.TrafficEntities.DeleteObject(entity);
                context.SaveChanges();
            }
        }
    }
}
